package cn.edu.neusoft.food729.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.view.menu.ActionMenuItemView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import com.example.retroiftdemo_729.R;
import com.squareup.picasso.Picasso;

import java.util.List;

import cn.edu.neusoft.food729.Bean.FoodListBean;
import cn.edu.neusoft.food729.adapter.FoodListAdapter;
import cn.edu.neusoft.food729.common.Common;
import cn.edu.neusoft.food729.listener.CollectListener;
import cn.edu.neusoft.food729.listener.FoodListListener;
import cn.edu.neusoft.food729.model.CollectModel;
import cn.edu.neusoft.food729.model.FoodListModel;

public class FoodListActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private LinearLayoutManager layoutManager;
    private FoodListAdapter myAdapter;
    private int page = 1, shop_id, flag = 0;
    List<FoodListBean> list;

    ImageView imageView;
    TextView shopname, adress, tel, intro;
    RatingBar bar;

    Context context;

    private boolean flagcollect = false;//收藏标志
    private SharedPreferences sp;//简单存储
    private String sessionID = "";
    private ActionMenuItemView item;
    private CollectModel collectModel;

    private FoodListListener listener = new FoodListListener() {
        @Override
        public void onResponse(List<FoodListBean> beanlist) {
            if (page == 1) {
                list = beanlist;
            } else {
                list.removeAll(beanlist);
                list.addAll(beanlist);
            }
            myAdapter.setList(list);
            recyclerView.setAdapter(myAdapter);
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(FoodListActivity.this, msg, Toast.LENGTH_SHORT).show();
        }
    };
    CollectListener collectListener = new CollectListener() {
        @SuppressLint("RestrictedApi")
        @Override
        public void onResponse(String msg) {
            if (item == null)
                item = findViewById(R.id.menucollect);
            switch (msg) {
                case "collect":
//                    System.out.println("----收藏成功");
                    Toast.makeText(FoodListActivity.this, "收藏成功", Toast.LENGTH_SHORT).show();
                    flagcollect = true;
                    item.setIcon(getDrawable(R.drawable.ic_shoucang_1));
                    break;
                case "collected":
//                    System.out.println("----已收藏");
                    Toast.makeText(FoodListActivity.this, "已收藏", Toast.LENGTH_SHORT).show();
                    flagcollect = true;
                    item.setIcon(getDrawable(R.drawable.ic_shoucang_1));
                    break;
                case "unCollect":
//                    System.out.println("----未收藏");
                    Toast.makeText(FoodListActivity.this, "未收藏", Toast.LENGTH_SHORT).show();
                    flagcollect = false;
                    item.setIcon(getDrawable(R.drawable.ic_shoucang));
                    break;
                case "collectDelete":
//                    System.out.println("----取消收藏");
                    Toast.makeText(FoodListActivity.this, "取消收藏", Toast.LENGTH_SHORT).show();
                    flagcollect = false;
                    item.setIcon(getDrawable(R.drawable.ic_shoucang));
                    break;
                default:
                    Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
            }

        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
        }
    };

    private void init() {
        recyclerView = findViewById(R.id.recyclerView);

        layoutManager = new LinearLayoutManager(this);
        //准备数据
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setHasFixedSize(true);
        myAdapter = new FoodListAdapter(this);
        recyclerView.setAdapter(myAdapter);
//        myAdapter.setList(list);
        imageView = findViewById(R.id.food_image);
        shopname = findViewById(R.id.res_name);
        adress = findViewById(R.id.food_price);
        tel = findViewById(R.id.tel);
        intro = findViewById(R.id.intro);
        bar = findViewById(R.id.ratingBar1);

        String image = getIntent().getStringExtra("image");
        Picasso.with(this)
                .load(Common.BASEURL + image)
                .into(imageView);

        shopname.setText(getIntent().getStringExtra("shop_name"));
        adress.setText(getIntent().getStringExtra("shop_adress"));
        tel.setText(getIntent().getStringExtra("tel"));
        intro.setText(getIntent().getStringExtra("intro"));
        bar.setRating(getIntent().getIntExtra("level", -1));
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_food_list);
        context = FoodListActivity.this;
        init();
        FoodListModel model = new FoodListModel();
        String shop_name = getIntent().getStringExtra("shop_name");
        setTitle(shop_name);

        shop_id = getIntent().getIntExtra("shop_id", -1);
        model.getResultList(shop_id, listener);

        sp = context.getSharedPreferences("login", MODE_PRIVATE);
        readSP();

    }

    private void readSP() {
        sessionID = sp.getString("session", null);//注意名值对的名
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menuc, menu);
        collectModel = new CollectModel();
        collectModel.exists(sessionID, shop_id, flag, collectListener);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menucollect:
                if (flagcollect) {
                    System.out.println("----准备取消收藏");
                    collectModel.uncollectShop(sessionID, shop_id, collectListener);
                } else {
                    System.out.println("----准备收藏");
                    collectModel.collectShop(sessionID, shop_id, collectListener);
                }
                break;
        }
        return super.onOptionsItemSelected(item);
    }

}

